package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/*
* 部门管理Controller
* */
@RestController
@Slf4j
@RequestMapping("/depts")
public class DeptController {
    //private static Logger log = LoggerFactory.getLogger(DeptController.class);

    @Resource
    private DeptService deptService;

    /*
    * 查询部门数据
    * */
    // @RequestMapping(value = "/depts", method = RequestMethod.GET)
    @GetMapping
    public Result list() {
        log.info("查询全部部门数据");

        // 调用service查询部门数据
        List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }

    /*
    * 删除部门
    * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {

        log.info("根据id删除部门：{}", id);

        // 调用service删除部门
        deptService.delete(id);

        return Result.success("删除成功");
    }


    /*
    * 新增部门
    * */
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("添加部门：{}", dept);

        // 调用service添加部门
        deptService.add(dept);

        return Result.success("添加成功");
    }


    /*
    * 根据ID查询部门
    * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询部门");

        Dept result = deptService.getById(id);

        return Result.success(result);
    }

    /*
    * 修改部门
    * */
    @PutMapping
    public Result modify(@RequestBody Dept dept) {
        log.info("修改部门名称");

        System.out.println("dept：" + dept);

        deptService.modify(dept);

        return Result.success("修改成功");
    }

}
